<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="售后单号" prop="sn">
        <el-input v-model="queryParams.sn" placeholder="请输入售后单据编号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderSn">
        <el-input v-model="queryParams.orderSn" placeholder="请输入订单编号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="采购商" prop="purchaserName">
        <el-input v-model="queryParams.purchaserName" placeholder="请输入采购商名称" clearable
          style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="售后类型" prop="serviceType">
        <el-select v-model="queryParams.serviceType" placeholder="售后类型" clearable
          style="width: 240px">
          <el-option v-for="dict in dict.type.order_after_sale" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="售后状态" prop="serviceStatus">
        <el-select v-model="queryParams.serviceStatus" placeholder="售后状态" clearable
          style="width: 240px">
          <el-option v-for="dict in dict.type.after_sale_status" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-select v-model="queryParams.paymentMethod" placeholder="支付方式" clearable multiple
          style="width: 240px">
          <el-option v-for="dict in dict.type.pay_way" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="退款时间">
        <el-date-picker v-model="refundDateRange" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
          @click="handleEditTable(undefined)" v-hasPermi="['system:role:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleEditTable" v-hasPermi="['system:role:edit']">修改</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['order:afterSale:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns">
      </right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afterSaleList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="columns[0].label" :prop="columns[0].prop" :width="columns[0].width"
        v-if="columns[0].visible" />
      <el-table-column :label="columns[1].label" :prop="columns[1].prop"
        :show-overflow-tooltip="true" :width="columns[1].width" v-if="columns[1].visible" />
      <el-table-column :label="columns[2].label" :prop="columns[2].prop"
        :show-overflow-tooltip="true" :width="columns[2].width" v-if="columns[2].visible" />
      <el-table-column :label="columns[3].label" :prop="columns[3].prop"
        :show-overflow-tooltip="true" :width="columns[3].width" v-if="columns[3].visible" />
      <el-table-column :label="columns[4].label" :prop="columns[4].prop" align="center"
        :show-overflow-tooltip="true" :width="columns[4].width" v-if="columns[4].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_after_sale" :value="scope.row.serviceType" />
        </template>
      </el-table-column>
      <el-table-column :label="columns[5].label" :prop="columns[5].prop"
        :show-overflow-tooltip="true" :width="columns[5].width" v-if="columns[5].visible" />
      <el-table-column :label="columns[6].label" :prop="columns[6].prop" align="left"
        :show-overflow-tooltip="true" :width="columns[6].width" v-if="columns[6].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.after_sale_status" :value="scope.row.serviceStatus" />
        </template>
      </el-table-column>
      <el-table-column :label="columns[7].label" :prop="columns[7].prop"
        :show-overflow-tooltip="true" :width="columns[7].width" v-if="columns[7].visible" />
      <el-table-column :label="columns[8].label" :prop="columns[8].prop" align="left"
        :show-overflow-tooltip="true" :width="columns[8].width" v-if="columns[8].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_way" :value="scope.row.paymentMethod" />
        </template>
      </el-table-column>
      <el-table-column :label="columns[9].label" :prop="columns[9].prop" align="left"
        :show-overflow-tooltip="true" :width="columns[9].width" v-if="columns[9].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_way" :value="scope.row.paymentChannel" />
        </template>
      </el-table-column>
      <el-table-column :label="columns[10].label" :prop="columns[10].prop"
        :show-overflow-tooltip="true" :width="columns[10].width" v-if="columns[10].visible" />
      <el-table-column :label="columns[11].label" :prop="columns[11].prop"
        :show-overflow-tooltip="true" :width="columns[11].width" v-if="columns[11].visible" />
      <el-table-column :label="columns[12].label" :prop="columns[12].prop"
        :show-overflow-tooltip="true" :width="columns[12].width" v-if="columns[12].visible" />
      <el-table-column :label="columns[13].label" :prop="columns[13].prop"
        :show-overflow-tooltip="true" :width="columns[13].width" v-if="columns[13].visible" />
      <el-table-column :label="columns[14].label" :prop="columns[14].prop"
        :show-overflow-tooltip="true" :width="columns[14].width" v-if="columns[14].visible" />
      <el-table-column :label="columns[15].label" :prop="columns[15].prop"
        :show-overflow-tooltip="true" :width="columns[15].width" v-if="columns[15].visible" />
      <el-table-column :label="columns[16].label" :prop="columns[16].prop"
        :show-overflow-tooltip="true" :width="columns[16].width" v-if="columns[16].visible" />
      <el-table-column :label="columns[17].label" align="center" :prop="columns[17].prop"
        :show-overflow-tooltip="true" :width="columns[17].width" v-if="columns[17].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="columns[18].label" :prop="columns[18].prop"
        :show-overflow-tooltip="true" :width="columns[18].width" v-if="columns[18].visible" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEditTable(scope.row)"
            v-hasPermi="['order:afterSale:query']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 打开商品SKU促销状态对话框 -->
    <el-dialog v-dialogDragWidth v-dialogDragHeight v-dialogDrag title="确认商品促销价格" :visible.sync="openPromotionFlag" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col>
            <el-form-item label="促销价格" prop="promotionPrice">
              <el-input-number v-model="form.promotionPrice" controls-position="left" :min="0.01" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary"
          @click="handlePromotionStatusChange(currentRow, form.promotionPrice)">确 定</el-button>
        <el-button @click="closePromotionFlagView">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listAfterSale } from "@/api/order/afterSale";

export default {
  name: "Refund",
  dicts: ['order_after_sale', 'after_sale_status', 'pay_way'],
  data () {
    return {
      // 遮罩层
      loading: true,
      openPromotionFlag: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      afterSaleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      //退款时间
      refundDateRange: [],
      // 日期范围
      dateRange: [],
      // 列信息
      columns: [
        { key: 0, label: `单据编号`, prop: 'sn', width: 240, visible: false },
        { key: 1, label: `订单编号`, prop: 'orderSn', width: 240, visible: true },
        { key: 2, label: `采购商名称`, prop: 'purchaserName', width: 210, visible: true },
        { key: 3, label: `货物状态`, prop: 'receiptName', width: 80, visible: false },
        { key: 4, label: `售后类型`, prop: 'serviceType', width: 75, visible: true },
        { key: 5, label: `申请金额`, prop: 'applyRefundPrice', width: 75, visible: true },
        { key: 6, label: `单据状态`, prop: 'serviceStatus', width: 100, visible: true },
        { key: 7, label: `支付金额`, prop: 'flowPrice', width: 85, visible: true },
        { key: 8, label: `支付方式`, prop: 'paymentMethod', width: 85, visible: true },
        { key: 9, label: `支付通道`, prop: 'paymentChannel', width: 85, visible: true },
        { key: 10, label: `实际退款`, prop: 'actualRefundPrice', width: 80, visible: true },
        { key: 11, label: `退款时间`, prop: 'refundTime', width: 150, visible: true },
        { key: 12, label: `物流公司`, prop: 'mLogisticsName', width: 100, visible: false },
        { key: 13, label: `发货单号`, prop: 'mLogisticsNo', width: 100, visible: false },
        { key: 14, label: `买家发货时间`, prop: 'mDeliverTime', width: 100, visible: false },
        { key: 15, label: `申请原因`, prop: 'reason', width: 150, visible: true },
        { key: 16, label: `问题描述`, prop: 'problemDesc', width: 100, visible: false },
        { key: 17, label: `申请时间`, prop: 'createTime', width: 150, visible: true },
        { key: 18, label: `联系电话`, prop: "consigneeMobile", width: 130, visible: false },
      ],
      // 商品分类列表
      categoryOptions: [],
      // 当前行数据
      currentRow: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sn: undefined,
        orderSn: undefined,
        purchaserName: undefined,
        serviceType: undefined,
        serviceStatus: undefined,
        paymentMethod: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        promotionPrice: [
          { required: true, message: "促销价格不能为空", trigger: "blur" }
        ],
        roleKey: [
          { required: true, message: "权限字符不能为空", trigger: "blur" }
        ],
        roleSort: [
          { required: true, message: "角色顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList () {
      this.loading = true;

      listAfterSale(this.addDateRangeWithMap(this.addDateRangeWithMap(this.queryParams, this.dateRange, 'CreateTime'), this.refundDateRange, 'RefundTime')).then(response => {
        this.afterSaleList = response.rows;
        this.afterSaleList.forEach(afterSale => {
          if (afterSale.receipt) {
            afterSale.receiptName = "已收到货";
          } else {
            afterSale.receiptName = "未收到货";
          }
        });

        this.total = response.total;
        this.loading = false;
      }
      )
    },
    //商品促销状态窗口 打开
    openPromotionFlagView (row) {
      if (row.promotionFlag) {
        if (!row.promotionPrice) {
          row.promotionPrice = row.price
        }
        this.form.promotionPrice = row.promotionPrice;

        this.currentRow = row;
        this.openPromotionFlag = true;
      } else {
        this.handlePromotionStatusChange(row, row.promotionPrice)
      }
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        point: 0,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.refundDateRange = [];
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 操作 详情  */
    handleEditTable (row) {
      //const params = { id: row.id }
      //this.$tab.openPage('退货退款详情', '/order/refund-sku-edit/returnRefundDetails', params);

      this.$router.push("/order/after-sale-detail/" + row.id);
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('order/afterSale/export', {
        ...this.addDateRangeWithMap(this.addDateRangeWithMap(this.queryParams, this.dateRange, 'CreateTime'), this.refundDateRange, 'RefundTime')
      }, `售后单_${this.parseTime(new Date().getTime(), "{y}-{m}-{d}_{h}:{i}:{s}")}.xlsx`)
    },
    // 取消
    closePromotionFlagView () { },
  }
};
</script>
